<template>
	<div class="app-container">
		<el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch"
			label-width="68px">


			<el-form-item label="订单号" prop="orderNo">
				<el-input v-model="queryParams.orderNo" placeholder="请输入订单号" clearable
					@keyup.enter.native="handleQuery" />
			</el-form-item>

			<el-form-item label="退款状态" prop="auditStatus">
				<el-select v-model="queryParams.auditStatus" placeholder="请选择退款状态" clearable>
					<el-option v-for="dict in dict.type.refund_order_status" :key="dict.value" :label="dict.label"
						:value="dict.value" />
				</el-select>
			</el-form-item>

			<el-form-item>
				<el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
				<el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
			</el-form-item>
		</el-form>

		<el-table v-loading="loading" :data="refundOrderList" @selection-change="handleSelectionChange">
			<el-table-column type="selection" width="55" align="center" />
			<el-table-column label="ID" align="center" prop="id" />
			<el-table-column label="订单号" align="center" prop="orderNo" />
			<el-table-column label="项目" align="center" prop="aptitudePicture" width="200">
				<template slot-scope="scope">
					<div class="row">
						<image-preview :src="scope.row.itemPicture" :width="50" :height="50" />
						<div class="fourSelectInfo ">
							<div class="fourName ellipsis">{{scope.row.itemName}}</div>
							<div class="fourTime">时长：{{scope.row.itemTime}}分钟</div>
							<div class="rowFlex">
								<div class="fourTime fourPrice">￥{{scope.row.orderItemPrice}}</div>
								<span class="fourTime">x{{scope.row.orderNum}}</span>
							</div>
						</div>
					</div>
				</template>
			</el-table-column>
			<el-table-column label="订单总金额" align="center" prop="orderTotalPrice" />
			<el-table-column label="用户" align="center" prop="userName" />
			<el-table-column label="技师" align="center" prop="tname" />
			<el-table-column label="申请退款金额" align="center" prop="applyRefundPrice" />
			<el-table-column label="可退款金额" align="center" prop="canRefundPrice" />
			<el-table-column label="申请退款原因" align="center" prop="applyRefundReason" />
			<el-table-column label="补充描述" align="center" prop="addDescription" />
			<el-table-column label="拒绝原因" align="center" prop="falseReason" />
			<el-table-column label="退款状态" align="center" prop="auditStatus">
				<template slot-scope="scope">
					<dict-tag :options="dict.type.refund_order_status" :value="scope.row.auditStatus" />
				</template>
			</el-table-column>

			<el-table-column label="申请时间" align="center" prop="createTime" width="180">
				<template slot-scope="scope">
					<span>{{ parseTime(scope.row.createTime) }}</span>
				</template>
			</el-table-column>
			<el-table-column label="操作" align="left" class-name="small-padding fixed-width">
				<template slot-scope="scope">
					<el-button size="small" type="primary" plain @click="handleRouter(scope.row)">查看</el-button>
					<div v-if="scope.row.auditStatus==0">
						<el-button size="small" type="danger" plain @click="handleUpdate(scope.row , 1)">拒绝退款</el-button>
						<el-button size="small" type="success" plain @click="handleUpdate(scope.row , 2)">立即退款</el-button>
					</div>
					
				</template>
			</el-table-column>
		</el-table>

		<pagination v-show="total>0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize"
			@pagination="getList" />

		<!-- 添加或修改退款订单对话框 -->
		<el-dialog :title="title" :visible.sync="open" width="600px" append-to-body center>
			<el-form ref="form" :model="form" :rules="rules" label-width="80px" v-if="digLogType==1">
				<el-form-item label="拒绝原因" prop="falseReason">
					<el-input v-model="form.falseReason" placeholder="请输入拒绝原因" />
				</el-form-item>
			</el-form>

			<el-form ref="form" :model="form" :rules="rules" label-width="80px" v-if="digLogType==2">
				<div class="row">
					<image-preview :src="form.itemPicture" :width="60" :height="60" />
					<div class="formDigles">
						<div class="rowFlex mg-mb">
							<div class="fourName">{{form.itemName}}</div>
							<div class="digTips">待接单</div>
						</div>
						<div class="rowFlex">
							<div class="digTips">￥{{form.orderItemPrice}}</div>
							<div class="fourName">x{{form.orderNum}}</div>
						</div>
					</div>
				</div>

				<div class="row digOrdInp">
					<div class="row">
						<div class="fourName">项目金额：</div>
						<el-input v-model="form.orderItemPrice" size="small" placeholder="请输入" style="width: 80px;" />
					</div>
					<div class="fourName digOrdNum">
						退款数量：{{form.orderNum}}
					</div>
				</div>

				<div class="row digOrdInp">
					<div class="fourName row">
						技师车费：
						<el-input v-model="form.orderFarePrice" size="small" placeholder="请输入" style="width: 80px;" />
					</div>
				</div>
				
				<div class="row digOrdInp">
					<div class="fourName row">
						技师获得：
						<el-input v-model="form.technicianGet" size="small" placeholder="请输入" style="width: 80px;" />
					</div>
				</div>

				<div class="diagePrice">
					总计退款金额：
					<span class="digTips">
						￥{{form.orderItemPrice*form.orderNum+form.orderFarePrice}}
					</span>
				</div>
			</el-form>
			<div slot="footer" class="dialog-footer">
				<el-button type="primary" @click="submitForm" class="elBut">提 交</el-button>
				<el-button @click="cancel" class="elBut">取 消</el-button>
			</div>
		</el-dialog>
	</div>
</template>

<script>
	import {
		listRefundOrder,
		getRefundOrder,
		delRefundOrder,
		addRefundOrder,
		updateRefundOrder,
		refusOrder,
		agreeOrder
	} from "@/api/refundOrder/refundOrder";

	export default {
		name: "RefundOrder",
		dicts: ['pay_type', 'refund_order_status'],
		data() {
			return {
				// 遮罩层
				loading: true,
				// 选中数组
				ids: [],
				// 非单个禁用
				single: true,
				// 非多个禁用
				multiple: true,
				// 显示搜索条件
				showSearch: true,
				// 总条数
				total: 0,
				// 退款订单表格数据
				refundOrderList: [],
				// 弹出层标题
				title: "",
				// 是否显示弹出层
				open: false,
				// 查询参数
				queryParams: {
					pageNum: 1,
					pageSize: 10,
					userId: null,
					tid: null,
					refundOrderNo: null,
					orderNo: null,
					userOrderStatus: null,
					technicianOrderStatus: null,
					falseReason: null,
					auditStatus: null,
					itemName: null,
				},
				// 表单参数
				form: {},
				// 表单校验
				rules: {},
				digLogType: null
			};
		},
		created() {
			this.getList();
		},
		methods: {
			/** 查询退款订单列表 */
			getList() {
				this.loading = true;
				listRefundOrder(this.queryParams).then(response => {
					this.refundOrderList = response.rows;
					this.total = response.total;
					this.loading = false;
				});
			},
			// 取消按钮
			cancel() {
				this.open = false;
				this.reset();
			},
			// 表单重置
			reset() {
				this.form = {
					id: null,
					userId: null,
					tid: null,
					refundOrderNo: null,
					orderNo: null,
					applyRefundPrice: null,
					applyRefundItemPrice: null,
					applyRefundFarePrice: null,
					canRefundPrice: null,
					canRefundItemPrice: null,
					canRefundFarePrice: null,
					technicianGet: null,
					applyRefundReason: null,
					addDescription: null,
					userOrderStatus: null,
					technicianOrderStatus: null,
					falseReason: null,
					auditStatus: null,
					itemName: null,
					itemPicture: null,
					itemTime: null,
					orderTotalPrice: null,
					orderItemPrice: null,
					orderFarePrice: null,
					orderNum: null,
					payType: null,
					isDelete: null,
					updateTime: null,
					createTime: null
				};
				this.resetForm("form");
			},
			/** 搜索按钮操作 */
			handleQuery() {
				this.queryParams.pageNum = 1;
				this.getList();
			},
			/** 重置按钮操作 */
			resetQuery() {
				this.resetForm("queryForm");
				this.handleQuery();
			},
			// 多选框选中数据
			handleSelectionChange(selection) {
				this.ids = selection.map(item => item.id)
				this.single = selection.length !== 1
				this.multiple = !selection.length
			},
			/** 新增按钮操作 */
			handleAdd() {
				this.reset();
				this.open = true;
				this.title = "添加退款订单";
			},
			/** 修改按钮操作 */
			handleUpdate(row, index) {
				this.digLogType = index
				this.reset();
				const id = row.id || this.ids
				getRefundOrder(id).then(response => {
					this.form = response.data;
					this.open = true;
					index == 1 ? this.title = "拒绝退款" : this.title = "同意退款"
				});
			},
			handleRouter(row) {
				this.reset();
				const id = row.id || this.ids
				this.$commonJs.changeView('/refundOrder/refundOrder/edit', {
					id: id
				})
			},
			/** 提交按钮 */
			submitForm() {
				if (this.digLogType == 1) { //拒绝
					refusOrder({
						id: this.form.id,
						refuseReason: this.form.falseReason
					}).then(response => {
						this.$modal.msgSuccess("提交成功");
						this.open = false;
						this.getList();
					});
				}
				console.log(this.form.id,this.form.falseReason,this.form.refundCarPrice,this.form.technicianGet)

				if (this.digLogType == 2) { //同意
					agreeOrder({
						id: this.form.id,
						refundItemPrice: this.form.orderItemPrice,
						refundCarPrice:this.form.orderFarePrice,
						technicianGet:this.form.technicianGet
					}).then(response => {
						this.$modal.msgSuccess("提交成功");
						this.open = false;
						this.getList();
					});
				}


				// this.$refs["form"].validate(valid => {
				// 	if (valid) {
				// 		if (this.form.id != null) {
				// 			updateRefundOrder(this.form).then(response => {
				// 				this.$modal.msgSuccess("修改成功");
				// 				this.open = false;
				// 				this.getList();
				// 			});
				// 		} else {
				// 			addRefundOrder(this.form).then(response => {
				// 				this.$modal.msgSuccess("新增成功");
				// 				this.open = false;
				// 				this.getList();
				// 			});
				// 		}
				// 	}
				// });
			},
			/** 删除按钮操作 */
			handleDelete(row) {
				const ids = row.id || this.ids;
				this.$modal.confirm('是否确认删除退款订单编号为"' + ids + '"的数据项？').then(function() {
					return delRefundOrder(ids);
				}).then(() => {
					this.getList();
					this.$modal.msgSuccess("删除成功");
				}).catch(() => {});
			},
			/** 导出按钮操作 */
			handleExport() {
				this.download('refundOrder/refundOrder/export', {
					...this.queryParams
				}, `refundOrder_${new Date().getTime()}.xlsx`)
			}
		}
	};
</script>

<style lang="less" scoped>
	.fourSelectInfo {
		margin-left: 10px;
		text-align: left;
		flex: 1;



		.fourTime {
			color: #999;
			font-size: 10px;
		}

		.fourPrice {
			color: #f12c20;
		}
	}

	.fourName {
		font-size: 12px;
		color: #444;
	}

	.el-button {
		margin: 4px 0;
	}

	.elBut {
		margin: 0 20px;
	}

	.diagePrice {
		font-size: 12px;
		color: #444;
		border-top: 1px dashed #eee;
		padding-top: 20px;
	}

	.digTips {
		color: #f12c20;
		font-size: 12px;
	}


	.formDigles {
		flex: 1;
		margin-left: 10px;

		.mg-mb {
			margin-bottom: 16px;
		}
	}

	.digOrdInp {
		margin: 10px 0;

		.digOrdNum {
			margin-left: 20px;
		}
	}
</style>